import Vue from "vue";
import Vuex from 'vuex'
// 安装Vuex插件
Vue.use(Vuex);
// 创建一个store
const store = new Vuex.Store({
  state: {
    count: 0,
    str: 'hello'
  },
  // 同步
  mutations: {
    add(state, n) {
      state.count = state.count + 1 + (n || 0)
    },
    rev(state, payload) {
      state.str = payload.next
    }
  },
  // 可执行异步操作=》mutation 来记录 action 产生的副作用
  actions: {
    async addData({ commit }, payload) {
      console.log('context:', commit)
      setTimeout(() => commit('add', payload), 2000)

      // return new Promise((r) => {
      //   setTimeout(() => {
      //     commit('add', payload);
      //     r()
      //   }, 2000)
      // })

      // const res = await Promise.resolve(2);
      // commit('add', payload + res);
    }
  }
})

export default store